package org.example.algorithm.str;

public class LengthOfLastWordSolution {
    public static void main(String[] args) {
        String s = "Hello World la";
        int res = lengthOfLastWord(s);
        System.out.println(res);
    }

    public static int lengthOfLastWord(String s) {
        if (s == null) {
            return 0;
        }
        int count = 0;
        for (int i=s.length() - 1;i>=0;i--) {
            if (s.charAt(i) != ' ') {
                count++;
            } else if (count > 0) {
                return count;
            }
        }
        return count;
    }
}
